﻿/*
 * sysinfo.hpp
 *
 *  Created on: 2018年2月13日
 *      Author: work
 */

#ifndef _DM_OS_SYS_SYSINFO_HPP_
#define _DM_OS_SYS_SYSINFO_HPP_

#include <dm/export.hpp>

#ifndef DM_API_OS_SYS
#define DM_API_OS_SYS DM_API_IMPORT
#endif

#include <dm/os/sys/taskstate.hpp>
#include <dm/os/sys/taskinfo.hpp>
#include <dm/os/spinlock.hpp>
#include <dm/os/sys/process.hpp>

namespace dm{
namespace os{
namespace sys{

/**
 * 系统信息
 * 实现对系统任务状态的访问与控制。
 */
class DM_API_OS_SYS CSysInfo{
	CSysInfo();
protected:
	CSysInfo( const CSysInfo& info );

public:
	/**
	 * 系统信息
	 */
	struct SSysInfo{
		/**
		 * 任务数量
		 */
		int taskCount;

		dm::os::CSpinlock lock;

		/**
		 * 系统状态
		 */
		volatile ETaskState sysState;

		/**
		 * 开机时刻
		 */
		volatile dm::CTimeStamp startTs;

		/**
		 * 上次刷新时刻
		 */
		volatile dm::CTimeStamp refreshTs;

		/**
		 * 系统进程号
		 */
		volatile process_t pid;

		SSysInfo();
	};

public:
	static CSysInfo& ins();

	const SSysInfo* sysInfo()const;

	/**
	 * 复位系统
	 */
	static void reset();

	bool sysStartTs( dm::CTimeStamp& ts );
	bool sysRefreshTs( dm::CTimeStamp& ts );
	bool sysState( ETaskState& state );

	bool getSysInfo( dm::CTimeStamp& startTs,dm::CTimeStamp& refreshTs,ETaskState& state );

	bool ifSysRunning();

	void sysStop();

	int taskCount()const;

	int getTaskIndex( const char* program,const char* paras=NULL )const;

	const STaskInfo* getTaskInfo( const int& idx )const;
    STaskInfo* getTaskInfo( const int& idx );

	bool ifTaskExist( const int& idx );
	bool ifTaskRunning( const int& idx );

	void taskStop( const int& idx );

protected:
	void sysInit( const dm::CTimeStamp& now=dm::CTimeStamp::cur() );
	void sysRun( const dm::CTimeStamp& now=dm::CTimeStamp::cur() );

protected:
	friend class CTaskLoader;

	SSysInfo* m_info;
	STaskInfo* m_taskInfos;
};


}
}
}

#endif /* DM_RUNTIME_INCLUDE_DM_OS_SYS_SYSINFO_HPP_ */
